package com.hepinghospital.user.ui.activity.nursing;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gyf.immersionbar.ImmersionBar;
import com.hepinghospital.user.R;
import com.hepinghospital.user.adapter.AppointProjectAdapter;
import com.hepinghospital.user.base.BaseActivity;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.observer.BaseObserver;
import com.rest.response.NursingProjectResponse;

import org.jetbrains.annotations.NotNull;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.internal.DebouncingOnClickListener;

/**
 * @Description: 预约护理 项目列表
 * @Author: lzp
 * @CreateDate: 2022/3/3 14:49
 * @UpdateRemark: 更新说明
 */
public class AppointProjectListActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.cl_head)
    ConstraintLayout clHead;
    @BindView(R.id.rv_appoint_project)
    RecyclerView recyclerView;

    AppointProjectAdapter mAdapter;
    ArrayList<NursingProjectResponse.ChargeItemsBean> data = new ArrayList<>();
    static WeakReference<Activity> mActivity = null;

    @Override
    protected void initViews() {
        tvTitle.setText("护理预约");
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);

        mAdapter = new AppointProjectAdapter(data);
//        mAdapter.addHeaderView(getHeadView());

        recyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("item", data.get(position));
            startActivity(AppointStep1Activity.class, bundle);
        });
        clHead.setOnClickListener(v -> nurse_button_action());
    }

//    @NotNull
//    private View getHeadView() {
//        View headView = getLayoutInflater().inflate(R.layout.recycler_head_nurselist_layout, null);
//        RelativeLayout nuserhead_relative = headView.findViewById(R.id.nuserhead_relative);
//        nuserhead_relative.setOnClickListener(new DebouncingOnClickListener() {
//            @Override
//            public void doClick(View p0) {
//                nurse_button_action();
//            }
//        });
//        return headView;
//    }

    void nurse_button_action() {
        Bundle bundle = new Bundle();
        startActivity(AppointNurseListActivity.class, bundle);
    }


    @Override
    protected void doBeforeSetcontentView() {
        ImmersionBar.with(this).keyboardEnable(false).transparentStatusBar().fitsSystemWindows(false).navigationBarColor(R.color.black).autoDarkModeEnable(true).init();
    }


    @Override
    protected void initData() {
        mActivity = new WeakReference<>(this);
        showLoadingDialog(mContext);
        RestProxy.getInstance().getNursingItemLists("1", new BaseObserver<NursingProjectResponse>() {
            @Override
            public void onError(@NonNull Throwable e) {
                super.onError(e);
                ToastUtil.onError(AppointProjectListActivity.this, e);
                dismissLoadingDialog();
            }

            @Override
            public void onSuccess(NursingProjectResponse result) {
                dismissLoadingDialog();
                data.addAll(result.getChargeItems());
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_nursing_project_list;
    }

    public static void clsoeSelf() {
        if (mActivity != null && mActivity.get() != null) {
            Activity context = mActivity.get();
            context.finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mActivity != null) {
            mActivity.clear();
            mActivity = null;
        }
    }
}
